import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**巽风SDK信息属性枚举*/
export const enum E_XunFengSdk {
	/**用户ID*/
	USER_ID = 0,
	/**用户昵称*/
	NICKNAME = 1,
	/**用户头像*/
	ICON = 2,
	/**接口token*/
	API_TOKEN = 3,
	/**元贝*/
	COIN = 4,
	/**闪光元贝*/
	FLASH_COIN = 5,
	/**钻石*/
	DIAMOND = 6,
	/**兑换开关查询地址*/
	SWITCH_QUERY_URL = 7,
	/**枚举数量*/
	EnumCount = 8
}


/**巽风SDK信息*/
export class XunFengSdk {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_XUN_FENG_SDK_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_XUN_FENG_SDK_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: XunFengSdk[] = [];

	/**创建入口*/
	public static create(json?: string): XunFengSdk {
		const c = this.POOL.pop() || new XunFengSdk();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**用户ID*/
	protected _userId: string;
	/**用户昵称*/
	protected _nickname: string;
	/**用户头像*/
	protected _icon: string;
	/**接口token*/
	protected _apiToken: string;
	/**元贝 (uint32)*/
	protected _coin: number;
	/**闪光元贝 (uint32)*/
	protected _flashCoin: number;
	/**钻石 (uint32)*/
	protected _diamond: number;
	/**兑换开关查询地址*/
	protected _switchQueryUrl: string;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (XunFengSdk.POOL.indexOf(this) === -1) {
			XunFengSdk.POOL.push(this);
		}
	}

	/**用户ID*/
	public get userId() {
		return this._userId;
	}


	/**用户ID*/
	public set userId(v: string) {
		this._userId = v;
	}


	/**用户昵称*/
	public get nickname() {
		return this._nickname;
	}


	/**用户昵称*/
	public set nickname(v: string) {
		this._nickname = v;
	}


	/**用户头像*/
	public get icon() {
		return this._icon;
	}


	/**用户头像*/
	public set icon(v: string) {
		this._icon = v;
	}


	/**接口token*/
	public get apiToken() {
		return this._apiToken;
	}


	/**接口token*/
	public set apiToken(v: string) {
		this._apiToken = v;
	}


	/**元贝*/
	public get coin() {
		return this._coin;
	}


	/**元贝*/
	public set coin(v: number) {
		this._coin = v;
	}


	/**闪光元贝*/
	public get flashCoin() {
		return this._flashCoin;
	}


	/**闪光元贝*/
	public set flashCoin(v: number) {
		this._flashCoin = v;
	}


	/**钻石*/
	public get diamond() {
		return this._diamond;
	}


	/**钻石*/
	public set diamond(v: number) {
		this._diamond = v;
	}


	/**兑换开关查询地址*/
	public get switchQueryUrl() {
		return this._switchQueryUrl;
	}


	/**兑换开关查询地址*/
	public set switchQueryUrl(v: string) {
		this._switchQueryUrl = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_XunFengSdk): any {
		switch (k) {
			case E_XunFengSdk.USER_ID: return this._userId;
			case E_XunFengSdk.NICKNAME: return this._nickname;
			case E_XunFengSdk.ICON: return this._icon;
			case E_XunFengSdk.API_TOKEN: return this._apiToken;
			case E_XunFengSdk.COIN: return this._coin;
			case E_XunFengSdk.FLASH_COIN: return this._flashCoin;
			case E_XunFengSdk.DIAMOND: return this._diamond;
			case E_XunFengSdk.SWITCH_QUERY_URL: return this._switchQueryUrl;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_XunFengSdk, v: any): void {
		switch (k) {
			case E_XunFengSdk.USER_ID: this._userId = v; break;
			case E_XunFengSdk.NICKNAME: this._nickname = v; break;
			case E_XunFengSdk.ICON: this._icon = v; break;
			case E_XunFengSdk.API_TOKEN: this._apiToken = v; break;
			case E_XunFengSdk.COIN: this._coin = v; break;
			case E_XunFengSdk.FLASH_COIN: this._flashCoin = v; break;
			case E_XunFengSdk.DIAMOND: this._diamond = v; break;
			case E_XunFengSdk.SWITCH_QUERY_URL: this._switchQueryUrl = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//用户ID
		this._userId = '';
		//用户昵称
		this._nickname = '';
		//用户头像
		this._icon = '';
		//接口token
		this._apiToken = '';
		//元贝
		this._coin = 0;
		//闪光元贝
		this._flashCoin = 0;
		//钻石
		this._diamond = 0;
		//兑换开关查询地址
		this._switchQueryUrl = '';
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._userId) {
			json.userId = this._userId;
		}
		if (this._nickname) {
			json.nickname = this._nickname;
		}
		if (this._icon) {
			json.icon = this._icon;
		}
		if (this._apiToken) {
			json.apiToken = this._apiToken;
		}
		if (this._coin) {
			json.coin = this._coin;
		}
		if (this._flashCoin) {
			json.flashCoin = this._flashCoin;
		}
		if (this._diamond) {
			json.diamond = this._diamond;
		}
		if (this._switchQueryUrl) {
			json.switchQueryUrl = this._switchQueryUrl;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.userId) {
			this._userId = json.userId;
		}
		if (json.nickname) {
			this._nickname = json.nickname;
		}
		if (json.icon) {
			this._icon = json.icon;
		}
		if (json.apiToken) {
			this._apiToken = json.apiToken;
		}
		if (json.coin) {
			this._coin = json.coin;
		}
		if (json.flashCoin) {
			this._flashCoin = json.flashCoin;
		}
		if (json.diamond) {
			this._diamond = json.diamond;
		}
		if (json.switchQueryUrl) {
			this._switchQueryUrl = json.switchQueryUrl;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeString(this._userId);
		byte.writeString(this._nickname);
		byte.writeString(this._icon);
		byte.writeString(this._apiToken);
		byte.writeUint32(this._coin);
		byte.writeUint32(this._flashCoin);
		byte.writeUint32(this._diamond);
		byte.writeString(this._switchQueryUrl);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._userId = byte.readString();
		this._nickname = byte.readString();
		this._icon = byte.readString();
		this._apiToken = byte.readString();
		this._coin = byte.readUint32();
		this._flashCoin = byte.readUint32();
		this._diamond = byte.readUint32();
		this._switchQueryUrl = byte.readString();
		return this;
	}


	/**克隆*/
	public clone(): XunFengSdk {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = XunFengSdk.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = XunFengSdk.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//用户ID
			if (idx === E_XunFengSdk.USER_ID) {
				const oldv = this._userId;
				this._userId = us[i + 1];
				IAsync.dispatch(eventID, idx, this._userId, oldv);
				i += 2;
				continue;
			}
			//用户昵称
			if (idx === E_XunFengSdk.NICKNAME) {
				const oldv = this._nickname;
				this._nickname = us[i + 1];
				IAsync.dispatch(eventID, idx, this._nickname, oldv);
				i += 2;
				continue;
			}
			//用户头像
			if (idx === E_XunFengSdk.ICON) {
				const oldv = this._icon;
				this._icon = us[i + 1];
				IAsync.dispatch(eventID, idx, this._icon, oldv);
				i += 2;
				continue;
			}
			//接口token
			if (idx === E_XunFengSdk.API_TOKEN) {
				const oldv = this._apiToken;
				this._apiToken = us[i + 1];
				IAsync.dispatch(eventID, idx, this._apiToken, oldv);
				i += 2;
				continue;
			}
			//元贝
			if (idx === E_XunFengSdk.COIN) {
				const oldv = this._coin;
				this._coin = us[i + 1];
				IAsync.dispatch(eventID, idx, this._coin, oldv);
				i += 2;
				continue;
			}
			//闪光元贝
			if (idx === E_XunFengSdk.FLASH_COIN) {
				const oldv = this._flashCoin;
				this._flashCoin = us[i + 1];
				IAsync.dispatch(eventID, idx, this._flashCoin, oldv);
				i += 2;
				continue;
			}
			//钻石
			if (idx === E_XunFengSdk.DIAMOND) {
				const oldv = this._diamond;
				this._diamond = us[i + 1];
				IAsync.dispatch(eventID, idx, this._diamond, oldv);
				i += 2;
				continue;
			}
			//兑换开关查询地址
			if (idx === E_XunFengSdk.SWITCH_QUERY_URL) {
				const oldv = this._switchQueryUrl;
				this._switchQueryUrl = us[i + 1];
				IAsync.dispatch(eventID, idx, this._switchQueryUrl, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in XunFengSdk with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: XunFengSdk): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._userId !== other._userId) {
			updates.push(E_XunFengSdk.USER_ID, this._userId);
		}
		if (this._nickname !== other._nickname) {
			updates.push(E_XunFengSdk.NICKNAME, this._nickname);
		}
		if (this._icon !== other._icon) {
			updates.push(E_XunFengSdk.ICON, this._icon);
		}
		if (this._apiToken !== other._apiToken) {
			updates.push(E_XunFengSdk.API_TOKEN, this._apiToken);
		}
		if (this._coin !== other._coin) {
			updates.push(E_XunFengSdk.COIN, this._coin);
		}
		if (this._flashCoin !== other._flashCoin) {
			updates.push(E_XunFengSdk.FLASH_COIN, this._flashCoin);
		}
		if (this._diamond !== other._diamond) {
			updates.push(E_XunFengSdk.DIAMOND, this._diamond);
		}
		if (this._switchQueryUrl !== other._switchQueryUrl) {
			updates.push(E_XunFengSdk.SWITCH_QUERY_URL, this._switchQueryUrl);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(XunFengSdk.TOTAL_EVENT_ID, this.getV(0));
	}

}
